package com.plugin.NetworkConnectInfo;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.widget.Toast;
import android.content.pm.PackageManager;

import static android.content.Context.WIFI_SERVICE;

/**
 * This class echoes a string called from JavaScript.
 */
public class NetworkConnectInfoPlugin extends CordovaPlugin {

    private Activity mActivity;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        mActivity = this.cordova.getActivity();

        switch (action) {
            case "coolMethod":
                String message = args.getString(0);
                this.coolMethod(message, callbackContext);
                return true;

            case "requestPermission":
                this.requestPermission(mActivity);
                return true;

            case "getConnectWifiInfo":
                this.getConnectWifiInfo(callbackContext);
                return true;
        }
        return false;
    }

    private void coolMethod(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) {
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void requestPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

    private void getConnectWifiInfo(CallbackContext callbackContext) throws JSONException {
        Context context = this.cordova.getActivity().getApplicationContext();
        WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);

        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            @SuppressLint("HardwareIds") String macAddress = wifiInfo.getMacAddress();
            int networkId = wifiInfo.getNetworkId();
            String wifiSSID = wifiInfo.getSSID();
            String wifiBSSID = wifiInfo.getBSSID();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ipAddress", ipAddress);
            jsonObject.put("macAddress", macAddress);
            jsonObject.put("networkId", networkId);
            jsonObject.put("wifiSSID", wifiSSID);
            jsonObject.put("wifiBSSID", wifiBSSID);
            callbackContext.success(jsonObject);
            return;
        }

        callbackContext.error("wifiManager is null");
    }
}
